package com.ncloud.auth.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.auth.config.Constant;
import com.ncloud.auth.service.AuthService;
import com.ncloud.common.body.Result;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "自动化运维认证管理", description = "认证管理", tags = "认证管理",
	basePath="/",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class AuthController {
	
	@Autowired
	private AuthService authService;
	
	@ApiOperation(value="登录认证", notes="输入用户名和密码进行认证")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "json", value = "账号和密码", required = true, dataType = "JSONObject" )
	})
	@PostMapping(value = "/auth")
    public Result<?> auth(@RequestBody JSONObject authenticationRequest)
			throws Exception {
		JSONObject response = authService.login(authenticationRequest.getString("userName"),
				authenticationRequest.getString("password"));
		return ResultUtil.success(response);
	}
    
	@ApiOperation(value="token刷新", notes="刷新token认证信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-FRESH", value = "refresh_token", required = true, dataType = "String" )
	})
	@PutMapping("/auth/refresh")
    public Result<?> refresh(HttpServletRequest request) throws Exception{
		String refreshtoken = request.getHeader(Constant.HEAD_REFRESH_TOKEN);
    	String token = authService.refresh(refreshtoken);
    	return ResultUtil.success(token);
    }
    
	@ApiOperation(value="注销登出", notes="用户退出系统注销")
	@PutMapping("/auth/destroy")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "token", required = true, dataType = "String" )
	})
    public Result<?> destroy(HttpServletRequest request) throws Exception{
		String token = request.getHeader(Constant.HEAD_TOKEN);
    	authService.destroy(token);
    	return ResultUtil.success();
    }
	
	@ApiOperation(value="忘记密码", notes="根据账号找回密码")
	@ApiImplicitParams({
        @ApiImplicitParam(paramType="path", name = "userName", value = "账号", required = true, dataType = "String" )
	})
	@GetMapping("/auth/{userName}/forgetPassword")
	public Result<?> forgetpasswd(@PathVariable String userName) throws Exception{
		authService.forgetPassword(userName);
		return ResultUtil.success();
	}
	
	@ApiOperation(value="密码找回认证", notes="根据认证码进行校验链接是否有效")
	@ApiImplicitParams({
        @ApiImplicitParam(paramType="path", name = "authCode", value = "认证码", required = true, dataType = "String" )
	})
	@GetMapping("/auth/check/{authCode}")
	public Result<?> checkAuthCode(@PathVariable String authCode) throws Exception{
		Long userId = authService.checkAuthCode(authCode);
		return ResultUtil.success(userId);
	}
	
	@ApiOperation(value="重置密码", notes="根据认证码进行校验之后进行密码重置")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "json", value = "认证码，用户编码和新密码", required = true, dataType = "String" )
	})
	@PutMapping("/auth/resetPassword")
	public Result<?> resetPassword(@RequestBody JSONObject json) throws Exception{
		if(!json.containsKey("userId") || !json.containsKey("authCode") || !json.containsKey("password")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		authService.resetPassword(json.getLong("userId"),json.getString("authCode"),json.getString("password"));
		return ResultUtil.success();
	}
}
